<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>点赞特效</title>
    <style>
        .heart {
            background: url(../images/web_heart_animation.png);
            background-position: left;
            background-repeat: no-repeat;
            height: 100px;
            width: 100px;
            cursor: pointer;
            position: absolute;
            left:-14px;
            background-size:2900%;
        }
        .heart:hover, .heart:focus{
            background-position: right;
        }
        .likeCount{
            font-family: 'Georgia', Times, Times New Roman, serif;
            margin-top: 32px;
            margin-left: 68px;
            font-size: 25px;
            color: #999999
        }

        @-webkit-keyframes heartBlast {
            0% {
                background-position: left;
            }
            100% {
                background-position: right;
            }
        }
        @keyframes heartBlast {
            0% {
                background-position: left;
            }
            100% {
                background-position: right;
            }
        }
        .heartAnimation {
            display: inline-block;
            -webkit-animation-name: heartBlast;
            animation-name: heartBlast;
            -webkit-animation-duration: .8s;
            animation-duration: .8s;
            -webkit-animation-iteration-count: 1;
            animation-iteration-count: 1;
            -webkit-animation-timing-function: steps(28);//共28个背景图片帧
        animation-timing-function: steps(28);
            background-position: right;
        }
    </style>
</head>
<body>
<div class="feed" id="feed1">
    <p>W3C </p>
    <div class="heart " id="like1" rel="like"></div>
    <div class="likeCount" id="likeCount1">14</div>
</div>
<div class="feed" id="feed2">
    <p>百度一下</p>
    <div class="heart" id="like2" rel="like"></div>
    <div class="likeCount" id="likeCount2">10</div>
</div>
</body>
</html>
<script src="/web/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('body').on("click",'.heart',function(){
            var A=$(this).attr("id");
            var B=A.split("like");
            var messageID=B[1];
            var C=parseInt($("#likeCount"+messageID).html());
            var D=$(this).attr("rel");//如果为Like则执行动画
            if(D =='like')
            {
                $("#likeCount"+messageID).html(C+1);
                $(this).addClass("heartAnimation").attr("rel","unlike");
                $(this).css("background-position","right");
                //不加这句有bug，动画最后又会到了起点，点击后强制使图片停在最右边，
            }
            else
            {
                $("#likeCount"+messageID).html(C-1);
                $(this).removeClass("heartAnimation").attr("rel","like");
                $(this).css("background-position","left");
                //点击后强制使红心变黑，否则显示悬停状态的红色的心
            }
        });
    });
</script>
